<?php
class User extends Zend_Db_Table {
	/**
	 * 表名
	 * @var string
	 */
	protected $_name = "cc_user"; 
	/**
	 * 主健
	 * @var string
	 */
	protected $_id = "id";
	/**
	 * DB类
	 * @var class
	 */
	private $db;
	
	/**
	 * 用户登录ID
	 * @var string
	 */
	private $userid ; 
	/**
	 * 用户信息
	 * @var array
	 */
	private $userinfo;
	
	/**
	 * 分类名类
	 * @var class Column
	 */
	private $column;
	/**
	 * 分类名数组
	 * @var array
	 */
	private $columnArr;

	/**
	 * 用户角色类
	 * @var class Userrole
	 */
	private $userRole;
	/**
	 * 用户角色row
	 * @var array
	 */
	private $userRoleRow;
	/**
	 * 用户角色ID
	 * @var string 
	 */
	private $userRoleId;

	/**
	 * 用户角色权限类
	 * @var class Actrole
	 */
	private $actRole;
	/**
	 * 用户权限
	 * @var array
	 */
	private $userAct;

	
	
	/**
	 * 构造函数
	 * @param string $username 用户登录名
	 */
	public function __construct($username){
		$this->userid = $username;
		$this->db = Zend_Db_Table::getDefaultAdapter();
		parent::_setup();
		//取用户角色
		$this->userRole = new Userrole();
		$this->userRoleRow = $this->userRole->getUserRoleByUserId($this->userid);
		$this->userRoleId = $this->userRoleRow['roleid'];
		//取用户角色对应的权限
		$this->actRole = new Actrole();
		$this->userAct = $this->actRole->getActByRoleName($this->userRoleId);
		//取列名
		$this->column = new Column();
		$this->columnArr = $this->column->getColumn();
		//取用户信息
	}
	
	/**
	 * 用户登录验证
	 * @param string $userid 用户登录名
	 * @param string $passwd 用户登录密码
	 * @return bool
	 */
	public static function auth($username,$passwd){
		$db = Zend_Db_Table::getDefaultAdapter ();
		$select = $db->select();
		$select->from("cc_user")
				->where("username=?",$username)
				->order("username");
		$sql = $select->__toString();
		$result = $db->fetchAll($sql);
		if (empty($result)) {
			throw new Exception("用户不存在!");
		}
		if(trim($result[0]['password'])==md5($passwd)){
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 修改用户资料
	 * @access public
	 * @param array 用户修改数组
	 * @return 
	 */
	public function updUser($set,$id,$table="cc_user"){
		$where = $this->db->quoteInto('id=?',$id);
		$rowcnt = $this->update($set,$where);
		return $rowcnt;
	}
	
	/**
	 * 添加用户
	 * @access public
	 * @return 
	 */
	public function addUser($userArr,$table="cc_user"){
		$this->insert($userArr);
	}
	
	/**
	 * 删除用户
	 * @access public
	 * @return int
	 */
	public function delUser($id){
		$where = $this->db->quoteInto("id=?",$id);
		$lastid = $this->delete($where);
		return $lastid;
	}
	
	/**
	 * 取用户权限列表
	 * @access public
	 * @return array 
	 */
	public function getUserAct(){
		return $this->userAct;
	}
	
	/**
	 * 取列名
	 * @access public
	 * @return array 列名
	 */
	public function getColumn(){
		return $this->columnArr;
	}
	
	/**
	 * 取当前用户信息
	 * @access public
	 * @return array 当前user信息
	 */
	public function getUserInfo(){
		$where = $this->db->quoteInto("username=?",$this->userid);
		$row = $this->fetchRow($where);
		$this->userinfo = $row->toArray();
		return $this->userinfo;
	}
	
	/**
	 * 取所有用户列表
	 * @access public
	 * @return array 所有用户数组
	 */
	public function getUserList(){
		$rowset = $this->fetchAll();
		$userArr = $rowset->toArray();
		return $userArr;
	}
	
}
